package grgu.rud.glava2.b1;

import java.util.Random;
import java.util.Scanner;

public class B1 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Random rnd = new Random();
		System.out.print("Enter the dimension of the matrix: ");
		int n = sc.nextInt();
		int[][] a = new int[n][n];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[0].length; j++) {
				a[i][j] = -n + rnd.nextInt(2 * n);
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}

		System.out.print("Enter the column index (starting with 0): ");
		int k = sc.nextInt();

		for (int i = 0; i < a.length - 1; i++) {
			for (int j = i + 1; j < a[0].length; j++) {
				if (a[j][k] < a[i][k]) {
					int[] tempArr = a[i];
					a[i] = a[j];
					a[j] = tempArr;
				}
			}
		}

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a.length; j++) {
				System.out.print(a[i][j] + " ");
			}
			System.out.println();
		}
	}

}
